package com.sysu.dinner.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.sysu.dinner.model.Restaurant;
import com.sysu.dinner.service.RestaurantService;

@Component("indexAction")
@Scope("prototype")
public class IndexAction extends ActionSupport{
	private static final long serialVersionUID = 5553642021319869078L;
	
	private HttpSession session = null;
	private String name;
	private String role;
	private List<Restaurant> restaurants;
	
	private RestaurantService restaurantService;
	
	public IndexAction(){
		session = ServletActionContext.getRequest().getSession();
		if(session!=null && session.getAttribute("user")!=null ){
			setName(session.getAttribute("user").toString());
			setRole(session.getAttribute("role").toString());
		}
	}

	@Override
	public String execute() throws Exception {
		restaurants = restaurantService.getAllRestaurants();
		return "success";
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setRestaurants(List<Restaurant> restaurants) {
		this.restaurants = restaurants;
	}

	public List<Restaurant> getRestaurants() {
		return restaurants;
	}

	@Resource
	public void setRestaurantService(RestaurantService restaurantService) {
		this.restaurantService = restaurantService;
	}

	public RestaurantService getRestaurantService() {
		return restaurantService;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getRole() {
		return role;
	}

}
